package com.eax.CLILayer;
import com.eax.ControlLayer.DVDController;
import com.eax.ModelLayer.*;

import java.util.ArrayList;
import java.util.Scanner;

public class DVDUI {
	private com.eax.ControlLayer.DVDController dvdController; //DVD UI must know the DVD controller

	public DVDUI(DVDController dvdController) {
		this.dvdController = dvdController;
	}

	public void DVDMenu() {
		boolean exit = false;

		while(!exit) {
			//show menu and return the user's choice
			int choice = writeDVDMenu();

			switch (choice) {
				case 1:
					findDVD();
					break;
				case 2:
					createDVD();
					break;
				case 3:
					createCopy();
					break;
				case 4:
					deleteDVD();
					break;
				case 5:
					listAllDvds();
					break;
				default:
					exit = true;
					break;
			}

		}
	}

	private int writeDVDMenu()  {
		// creates an object keyboard to read data from the keyboard
		Scanner keyboard = new Scanner(System.in);
		System.out.println("\f *** DVD Menu ***");
		System.out.println(" (1) Show DVD information");
		System.out.println(" (2) Create DVD description");
		System.out.println(" (3) Create DVD specimen");
		System.out.println(" (4) Delete DVD");
		System.out.println(" (5) Show current DVD list");
		System.out.println(" (6) Return");
		System.out.print("\n Make your choice: ");

		return keyboard.nextInt();
	}

	private int inputId(){
		Scanner keyboard = new Scanner(System.in);
		System.out.print("Type DVD ID: " );
		return keyboard.nextInt();
	}

	private String inputTitle() {
		// creates an object keyboard to read data from the keyboard
		Scanner keyboard = new Scanner(System.in);
		System.out.print("Type DVD title: ");
		return keyboard.nextLine();
	}

	private String inputArtist() {
		// creates an object keyboard to read data from the keyboard
		Scanner keyboard = new Scanner(System.in);
		System.out.print("Type Main Artist name: ");
		return keyboard.nextLine();
	}

	private String inputReleaseDate() {
		// creates an object keyboard to read data from the keyboard
		Scanner keyboard = new Scanner(System.in);
		System.out.print("Type release date: ");
		return keyboard.nextLine();
	}

	private int inputCopySerialNumber() {
		// creates an object keyboard to read data from the keyboard
		Scanner keyboard = new Scanner(System.in);
		System.out.print("Type the copy serial number: ");
		return keyboard.nextInt();
	}

	private String inputDateOfPurchase() {
		// creates an object keyboard to read data from the keyboard
		Scanner keyboard = new Scanner(System.in);
		System.out.print("Type the date of purchase: ");
		return keyboard.nextLine();
	}

	private double inputPurchasePrice() {
		// creates an object keyboard to read data from the keyboard
		Scanner keyboard = new Scanner(System.in);
		System.out.print("Type the purchase price: ");
		return keyboard.nextDouble();
	}

	private void findDVD() {
		int id = inputId();
		DVDModel x = dvdController.findDVD(id);
		x.printInfo();
	}

	private void createDVD(){
		int id = dvdController.getHighestId() + 1;
		String title = inputTitle();
		String artist = inputArtist();
		String releaseDate = inputReleaseDate();

		dvdController.createDVD(id, title, artist, releaseDate);
		System.out.println(title + " has been added.\n");
	}

	private void createCopy() {
		int dvdId = 0;

		//returns a list of all dvds and their associated copies
		Boolean ok = false;
		ArrayList<DVDModel> dvds = dvdController.getAllDvds();
		int choice = 0;

		while(!ok){
			System.out.println("Please select a DVD to create a new copy for: ");

			for(DVDModel d : dvds){
				System.out.println(d.getId() + ") "+d.getTitle());
			}

			Scanner keyboard = new Scanner(System.in);
			System.out.print("Selection: ");
			choice = keyboard.nextInt();

			DVDModel dvd = dvdController.findDVD(choice);
			if(dvd != null){
				ok = true;
				dvdId = choice;
			}
		}

		int serialNumber = dvdController.getHighestSerialNumber() + 1;
		String dateOfPurchase = inputDateOfPurchase();
		double purchasePrice = inputPurchasePrice();
		String status = "available";
		Boolean available = true;

		dvdController.createCopy(serialNumber, dvdId, dateOfPurchase, purchasePrice, status, available);
	}

	private void deleteDVD() {
		int dvdId = 0;
		Boolean ok = false;
		ArrayList<DVDModel> dvds = dvdController.getAllDvds();
		int choice = 0;

		// Select which DVD should be deleted.
		while(!ok){
			System.out.println("Please select a DVD to remove, along with all copies: ");

			for(DVDModel d : dvds){
				System.out.println(d.getId() + ") "+d.getTitle());
			}

			Scanner keyboard = new Scanner(System.in);
			System.out.print("Selection: ");
			choice = keyboard.nextInt();

			DVDModel dvd = dvdController.findDVD(choice);
			if(dvd != null){
				ok = true;
				dvdId = choice;
			}
		}

		dvdController.deleteDVD(dvdId);
	}

	private void listAllDvds() {
		// List DVD's and all Copies.

		for(DVDModel d : dvdController.getAllDvds()){
			System.out.println("DVD: ");
			ArrayList<CopyModel> copies = dvdController.getAllCopies();

			d.printInfo();

			if(copies.size() > 0){
				System.out.println("Copies: ");
				for(CopyModel c : copies){
					if(c.getDvdId() == d.getId()){
						System.out.println(">>>>");
						c.printInfo();
						System.out.println("<<<<");
					}
				}
			}
		}
	}
}